import { Message } from 'iview';
import { findUser, addUser, delUser, updateUser } from '../../api/ServerApi';

const UserList = {
    namespaced: true,
    state() {
        return {
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                username: null,
                nickname: null,
                telephone: null,
                email: null,
                userType: null,
                ldapExist: null,
                locked: null,
                enabled: null,
                createStart: null,
                createEnd: null
            },
            tableLoading: false,
            tableData: {
                list: [],
                total: 0,
                pageNum: 1,
                pageSize: 10
            },
            editModal: {
                show: false,
                loading: false
            },
            editParams: {
                username: null,
                password: null,
                nickname: null,
                telephone: null,
                email: null,
                memberOf: null,
                locked: null,
                enabled: null,
                description: null
            },
            addModal: {
                show: false,
                loading: false
            },
            addParams: {
                username: null,
                nickname: null,
                password: null,
                passwordConfirm: null,
                telephone: null,
                email: null,
                memberOf: null,
                description: null
            }
        };
    },
    mutations: {
        setTableLoading(state, tableLoading) {
            state.tableLoading = tableLoading;
        },
        setTableData(state, tableData) {
            state.tableData = tableData;
        },
        setEditModal(state, editModal) {
            state.editModal = { ...state.editModal, ...editModal };
        },
        setAddModal(state, addModal) {
            state.addModal = { ...state.addModal, ...addModal };
        }
    },
    actions: {
        queryData({ commit, state }, initPage) {
            if (initPage === true) {
                state.queryParams.pageNo = 1;
                state.queryParams.pageSize = 10;
            }
            commit('setTableLoading', true);
            findUser(state.queryParams).then(data => {
                if (data.pageNum <= 0) {
                    data.pageNum = 1;
                }
                commit('setTableData', data);
                commit('setTableLoading', false);
            }).catch((error) => commit('setTableLoading', false));
        },
        deleteRow({ commit, dispatch }, { username }) {
            commit('setTableLoading', true);
            delUser(username).then(data => {
                Message.success({ content: `删除成功 -> [${username}]`, duration: 3 });
                dispatch('queryData');
            }).catch((error) => commit('setTableLoading', false));
        },
        updateRow({ commit, state, dispatch }) {
            commit('setEditModal', { loading: true });
            updateUser(state.editParams.username, state.editParams).then(data => {
                commit('setEditModal', { show: false, loading: false });
                Message.success({ content: `更新成功 -> [${state.editParams.username}]`, duration: 3 });
                dispatch('queryData');
            }).catch(error => commit('setEditModal', { loading: false }));
        },
        addRow({ commit, state, dispatch }) {
            commit('setAddModal', { loading: true });
            addUser(state.addParams).then(data => {
                commit('setAddModal', { show: false, loading: false });
                Message.success({ content: `新增成功 -> [${state.addParams.username}]`, duration: 3 });
                dispatch('queryData');
            }).catch(error => commit('setAddModal', { loading: false }));
        }
    }
};

export default UserList;
